/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "widget.h"
#include <QtMultimedia/QAudioDeviceInfo>
#include <QtMultimedia/QAudioInput>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QChart>
#include <QtWidgets/QVBoxLayout>
#include <QtCharts/QValueAxis>
#include "xyseriesiodevice.h"
#include <QTextStream>
#include <stdlib.h>
#include  <stdio.h>
#include <QTimer>
#include <QTime>
#include <QTextEdit>
QT_CHARTS_USE_NAMESPACE

Widget::Widget(QWidget *parent)
    : QWidget(parent),
      m_device(0),
      m_chart(0),
      m_series(0)
{
    /* Get and config chart */
    m_chart = new QChart;
    QChartView *chartView = new QChartView(m_chart);
    chartView->setMinimumSize(800, 600);
    m_series = new QLineSeries;
    m_chart->addSeries(m_series);
    QValueAxis *axisX = new QValueAxis;
    axisX->setRange(0, 3000);
    axisX->setLabelFormat("%g");
    axisX->setTitleText("Samples");
    QValueAxis *axisY = new QValueAxis;
    axisY->setRange(-1, 1);
    axisY->setTitleText("Voltage");
    m_chart->setAxisX(axisX, m_series);
    m_chart->setAxisY(axisY, m_series);
    m_chart->legend()->hide();
    m_chart->setTitle("Data From the Serialport");


    /* layout setting */
    /* add chart into mainwindow */
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(chartView);
    /* add  textEdit to Hlayout*/
    QHBoxLayout *textAndButtonLayout = new QHBoxLayout;
    _edit = new QTextEdit();
    textAndButtonLayout->addWidget(_edit);
    _sourceButton = new QPushButton("suspend");
    textAndButtonLayout->addWidget(_sourceButton);
    /* merge the two layout */
    mainLayout->addLayout(textAndButtonLayout);
    /* set mainlayout as the layout of the mainwindow */
    setLayout(mainLayout);

    /* set Button suspen */
    connect(_sourceButton, SIGNAL(clicked()), this, SLOT(sourceButtonSlot()));


    /* Init num */
    _data = new QByteArray();
    _data->append(1);
    /* Get IO device */
    m_device = new XYSeriesIODevice(m_series, this);
    m_device->open(QIODevice::ReadWrite);

    _dataSource = new DataSourceDevice(this,_data);

    /* Set timer */
    _timer = new QTimer(this);
    _timer->setInterval(100);
    /* set rand data source */
    // connect(_timer, SIGNAL(timeout()), this, SLOT(timerslotRand()));
    /* set serialport as data source */
    connect(_timer, SIGNAL(timeout()), this, SLOT(timerslotSerial()));
    _timer->start();
}

Widget::~Widget()
{
    _timer->stop();
    m_device->close();
}


void Widget::timerslotRand()
{
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    char  cc[3000] = {0};
    for(int i=0;i<3000;i+=300){
        char x = qrand();
        for(int j=0;j<300;j++)
            cc[i+j]=x;
    }
    m_device->writeDatax(cc,3000,_resolution);
}

void Widget::flashData()
{
    _dataSource->GetDataFromSerialport(_data->size());
    _datac = _data->data();
}

void Widget::timerslotSerial()
{
    _timer->stop();
    int oldsize = _data->size();
    flashData();
    int size = _data->size();

    if(size>1000)
        m_device->writeData(_datac,size);
    if(size>3000){
        int resize = size- (size/_resolution);
        *_data = _data->right(resize);
    }

    QString str,stri;
    for(int i=oldsize;i<size;i++){
        str = QString("%1").arg(_datac[i]&0xFF,1,10,QLatin1Char('0'));
        stri += " ";
        stri += str;
    }
    _edit->append(stri);

    _timer->start(100);
}

void Widget::sourceButtonSlot()
{
    QString text = _sourceButton->text();
    if(text == "suspend"){
        _timer->stop();
        _sourceButton->setText("go on");
    }else{
        _timer->start();
        _sourceButton->setText("suspend");
    }

}
